<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button>点击发送</button>
    <button>点击取消</button>

    <script>
      var btn = document.getElementsByTagName("button");

      let x = new XMLHttpRequest();

      btn[0].addEventListener("click", function () {
        x.open("GET", "http://127.0.0.1:7000/delay");

        x.send();

        x.onreadystatechange = function () {
          if (x.readyState === 4) {
            if (x.status >= 200 && x.status < 300) {
              console.log(x.response);
            }
          }
        };
      });

      btn[1].onclick = function () {
        x.abort();
        //借助abort方法，他是属于AJAX对象的
        //所以这个x应该在外部声明
      };
    </script>
  </body>
</html>
